﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Product;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Ekton_Manage_DTO.RequestModels.View;
using Ekton_Manage_InfrastructureCore.Repositories;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using Ekton_Manage_DomainCore;
using System.Linq;
using System.Collections.Generic;
using static Ekton_Manage_DTO.CommonModels.DtoFileModel;

namespace Ekton_Manage_Web.Controllers
{
    /// <summary>
    /// 产品分类
    /// </summary>
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class ProductController : ControllerBase
    {
        private readonly ISelectProductList _selectProductList;
        private readonly IBaseRepository<product_type, int> _baseproducttyperepository;
        private readonly IBaseRepository<AddProductList, int> _baseproductrepository;
        private readonly ApplicationDbContent _content;
        public ProductController(ISelectProductList selectProductList, IBaseRepository<product_type, int> baseproducttyperepository, IBaseRepository<AddProductList, int> baseproductrepository, ApplicationDbContent content)
        {
            _selectProductList = selectProductList;
            _baseproducttyperepository = baseproducttyperepository;
            _baseproductrepository = baseproductrepository;
            _content = content;
        }

        /// <summary>
        /// 查询产品分类
        /// </summary>
        /// 
        [HttpGet]
        public async Task<MessageModel<PageModel<product_type>>> GetProductCLassList([FromQuery] QueryDataParame query)
        {
            var result = await _selectProductList.SelectProductClassList(query);
            return new MessageModel<PageModel<product_type>>
            {
                msg = "数据获取成功",
                response = new PageModel<product_type>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }
        
        /// <summary>
        /// 查询产品列表
        /// </summary>
        /// 
        [HttpGet]
        public async Task<MessageModel<PageModel<product_view>>> GetProductList([FromQuery] QueryDataParame query)
        {
            var result = await _selectProductList.GetProductList(query);
            return new MessageModel<PageModel<product_view>>
            {
                msg = "数据获取成功",
                response = new PageModel<product_view>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }

        /// <summary>
        /// 编辑产品分类
        /// </summary>
        /// 
        [HttpPut]
        public async Task<Message> PutProductClass([FromBody] DtoSelectProductList prouderTypeId)
        {
            return await _selectProductList.PutProductClass(prouderTypeId);
        }


        /// <summary>
        /// 添加产品分类
        /// </summary>
        /// 
        [HttpPost]
        public async Task<Message> PostProductClass([FromBody] DtoSelectProductList request)
        {
            return await _selectProductList.PostProductClass(request);
        }

        /// <summary>
        /// 编辑产品列表
        /// </summary>
        /// 
        [HttpPut]
        public async Task<Message> PutProductList([FromBody] DtoInsertProductList request)
        {
            return await _selectProductList.PutProductList(request);
        }


        /// <summary>
        /// 添加产品列表
        /// </summary>
        /// 
        [HttpPost]
        public async Task<Message> PostProductList([FromBody] DtoInsertProductList request)
        {
            return await _selectProductList.PostProductList(request);
        }

        /// <summary>
        /// 删除产品列表
        /// </summary>
        /// 
        [HttpDelete]
        public async Task<Message> DeleteProductList(int id)
        {
            return await _selectProductList.DeleteProductList(id);
        }

        /// <summary>
        /// 删除产品分类
        /// </summary>
        /// 
        [HttpDelete]
        public async Task<Message> DeleteProductTypeList(int id)
        {
            return await _selectProductList.DeleteProductTypeList(id);
        }

        /// <summary>
        /// 导出产品分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<FileResult> ProductClassExportData([FromBody] ExportFormData formData)
        {
            List<product_type> data = new();
            if (formData.list.Count < 1)
                data = await _baseproducttyperepository.QueryTableList(m => true);
            else
                data = await _baseproducttyperepository.QueryTableList(m => formData.list.Contains(m.id)); 

            //创建一个新的excel文件
            HSSFWorkbook book = new HSSFWorkbook();
            //创建一个工作区
            ISheet sheet = book.CreateSheet("sheet1");
            //创建一行
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < 7; i++)
            {
                sheet.SetColumnWidth(i, 23 * 156);
            }
            sheet.SetColumnWidth(4, 30 * 156);
            sheet.SetColumnWidth(1, 21 * 156);
            //定义一个样式
            ICellStyle setborder = book.CreateCellStyle();
            setborder.BorderLeft = BorderStyle.Thin;
            setborder.BorderRight = BorderStyle.Thin;
            setborder.BorderBottom = BorderStyle.Thin;
            setborder.BorderTop = BorderStyle.Thin;

            //文字水平和垂直对齐方式
            setborder.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            setborder.Alignment = HorizontalAlignment.Center;//水平居中
            setborder.WrapText = true;//自动换行

            //再定义一个样式，用来设置最上面标题行的样式
            ICellStyle setborderdeth = book.CreateCellStyle();
            setborderdeth.BorderLeft = BorderStyle.Thin;
            setborderdeth.BorderRight = BorderStyle.Thin;
            setborderdeth.BorderBottom = BorderStyle.Thin;
            setborderdeth.BorderTop = BorderStyle.Thin;

            //定义一个字体样式
            IFont font = book.CreateFont();
            //将字体设为红色
            font.Color = IndexedColors.Red.Index;
            setborderdeth.SetFont(font);
            setborderdeth.VerticalAlignment = VerticalAlignment.Center;
            setborderdeth.Alignment = HorizontalAlignment.Center;
            setborderdeth.WrapText = true;

            row.HeightInPoints = 25;
            //设置单元格的值
            row.CreateCell(0).SetCellValue("序号");
            row.GetCell(0).CellStyle = setborderdeth;
            row.CreateCell(1).SetCellValue("产品图片");
            row.GetCell(1).CellStyle = setborderdeth;
            row.CreateCell(2).SetCellValue("产品分类名称");
            row.GetCell(2).CellStyle = setborderdeth;
            row.CreateCell(3).SetCellValue("分类级别");
            row.GetCell(3).CellStyle = setborderdeth;
            row.CreateCell(4).SetCellValue("父级分类");
            row.GetCell(4).CellStyle = setborderdeth;
            row.CreateCell(5).SetCellValue("产品分类描述");
            row.GetCell(5).CellStyle = setborderdeth;
            row.CreateCell(6).SetCellValue("创建人");
            row.GetCell(6).CellStyle = setborderdeth;
            row.CreateCell(7).SetCellValue("创建时间");
            row.GetCell(7).CellStyle = setborderdeth;
            for (int i = 0; i < data.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                row1.HeightInPoints = 21;
                row1.CreateCell(0).SetCellValue(i);
                row1.GetCell(0).CellStyle = setborder;
                row1.CreateCell(1).SetCellValue(data[i].product_picture);
                row1.GetCell(1).CellStyle = setborder;
                row1.CreateCell(2).SetCellValue(Convert.ToString(data[i].class_name));
                row1.GetCell(2).CellStyle = setborder;
                row1.CreateCell(3).SetCellValue(Convert.ToString(data[i].father_name));
                row1.GetCell(3).CellStyle = setborder;
                row1.CreateCell(4).SetCellValue(Convert.ToString(data[i].rank));
                row1.GetCell(4).CellStyle = setborder;
                row1.CreateCell(5).SetCellValue(Convert.ToString(data[i].product_describe));
                row1.GetCell(5).CellStyle = setborder;
                row1.CreateCell(6).SetCellValue(Convert.ToString(data[i].create_op_name));
                row1.GetCell(6).CellStyle = setborder;
                row1.CreateCell(7).SetCellValue(Convert.ToString(data[i].create_op_date));
                row1.GetCell(7).CellStyle = setborder;
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            DateTime dttime = DateTime.Now;
            string datetime = dttime.ToString("yyyy-MM-dd");
            string filename = "产品分类导出" + datetime + ".xls";
            return File(ms, "application/vns.ms-excel", filename);
        }

        /// <summary>
        /// 导出产品列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<FileResult> ProductExportData([FromBody] ExportFormData formData)
        {
            List<AddProductList> data = new();
            if (formData.list.Count < 1)
                data = await _baseproductrepository.QueryTableList(m => true);
            else
                data = await _baseproductrepository.QueryTableList(m => formData.list.Contains(m.id)); 

            //创建一个新的excel文件
            HSSFWorkbook book = new HSSFWorkbook();
            //创建一个工作区
            ISheet sheet = book.CreateSheet("sheet1");
            //创建一行
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < 8; i++)
            {
                sheet.SetColumnWidth(i, 23 * 156);
            }
            sheet.SetColumnWidth(4, 30 * 156);
            sheet.SetColumnWidth(1, 21 * 156);
            //定义一个样式
            ICellStyle setborder = book.CreateCellStyle();
            setborder.BorderLeft = BorderStyle.Thin;
            setborder.BorderRight = BorderStyle.Thin;
            setborder.BorderBottom = BorderStyle.Thin;
            setborder.BorderTop = BorderStyle.Thin;

            //文字水平和垂直对齐方式
            setborder.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            setborder.Alignment = HorizontalAlignment.Center;//水平居中
            setborder.WrapText = true;//自动换行

            //再定义一个样式，用来设置最上面标题行的样式
            ICellStyle setborderdeth = book.CreateCellStyle();
            setborderdeth.BorderLeft = BorderStyle.Thin;
            setborderdeth.BorderRight = BorderStyle.Thin;
            setborderdeth.BorderBottom = BorderStyle.Thin;
            setborderdeth.BorderTop = BorderStyle.Thin;

            //定义一个字体样式
            IFont font = book.CreateFont();
            //将字体设为红色
            font.Color = IndexedColors.Red.Index;
            setborderdeth.SetFont(font);
            setborderdeth.VerticalAlignment = VerticalAlignment.Center;
            setborderdeth.Alignment = HorizontalAlignment.Center;
            setborderdeth.WrapText = true;

            row.HeightInPoints = 25;
            //设置单元格的值
            row.CreateCell(0).SetCellValue("序号");
            row.GetCell(0).CellStyle = setborderdeth;
            row.CreateCell(1).SetCellValue("产品图片");
            row.GetCell(1).CellStyle = setborderdeth;
            row.CreateCell(2).SetCellValue("产品名称");
            row.GetCell(2).CellStyle = setborderdeth;
            row.CreateCell(3).SetCellValue("产品品牌");
            row.GetCell(3).CellStyle = setborderdeth;
            row.CreateCell(4).SetCellValue("产品分类");
            row.GetCell(4).CellStyle = setborderdeth;
            row.CreateCell(5).SetCellValue("产品描述");
            row.GetCell(5).CellStyle = setborderdeth;
            row.CreateCell(6).SetCellValue("备注信息");
            row.GetCell(6).CellStyle = setborderdeth;
            row.CreateCell(7).SetCellValue("创建人");
            row.GetCell(7).CellStyle = setborderdeth;
            row.CreateCell(8).SetCellValue("创建时间");
            row.GetCell(8).CellStyle = setborderdeth;
            for (int i = 0; i < data.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                row1.HeightInPoints = 21;
                row1.CreateCell(0).SetCellValue(i);
                row1.GetCell(0).CellStyle = setborder;
                row1.CreateCell(1).SetCellValue(Convert.ToString(data[i].product_picture));
                row1.GetCell(1).CellStyle = setborder;
                row1.CreateCell(2).SetCellValue(Convert.ToString(data[i].product_name));
                row1.GetCell(2).CellStyle = setborder;
                row1.CreateCell(3).SetCellValue(Convert.ToString(data[i].product_brand));
                row1.GetCell(3).CellStyle = setborder;
                row1.CreateCell(4).SetCellValue(Convert.ToString(data[i].class_name));
                row1.GetCell(4).CellStyle = setborder;
                row1.CreateCell(5).SetCellValue(Convert.ToString(data[i].product_describe));
                row1.GetCell(5).CellStyle = setborder;
                row1.CreateCell(6).SetCellValue(Convert.ToString(data[i].remark));
                row1.GetCell(6).CellStyle = setborder;
                row1.CreateCell(7).SetCellValue(Convert.ToString(data[i].create_op_name));
                row1.GetCell(7).CellStyle = setborder;
                row1.CreateCell(8).SetCellValue(Convert.ToString(data[i].create_op_date));
                row1.GetCell(8).CellStyle = setborder;
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            DateTime dttime = DateTime.Now;
            string datetime = dttime.ToString("yyyy-MM-dd");
            string filename = "产品列表导出" + datetime + ".xls";
            return File(ms, "application/vns.ms-excel", filename);
        }
    }
}
